package com.shop.cereshop.app.service.recommend.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cereshop.app.dao.recommend.CereUserBlockDAO;
import com.shop.cereshop.app.param.recommend.CereUserBlockDTO;
import com.shop.cereshop.app.service.recommend.CereUserBlockService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.recommend.CereUserBlock;
import com.shop.cereshop.commons.exception.CoBusinessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
* @author Administrator
* @description 针对表【cere_user_block(用户屏蔽表)】的数据库操作Service实现
* @createDate 2025-08-22 09:46:42
*/
@Service
public class CereUserBlockServiceImpl extends ServiceImpl<CereUserBlockDAO, CereUserBlock>
    implements CereUserBlockService{

    @Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = {CoBusinessException.class, Exception.class})
    @Override
    public void updateBlock(CereUserBlockDTO param, CereBuyerUser user) throws CoBusinessException {
        if (param.getBlockId() == null) {
            //查询用户是否屏蔽该目标
            CereUserBlock cereUserBlock = lambdaQuery()
                    .eq(CereUserBlock::getBlockUserId, user.getBuyerUserId())
                    .eq(CereUserBlock::getBlockTargetId, param.getBlockTargetId())
                    .one();
            if (cereUserBlock == null) {
                //新增
                CereUserBlock block = new CereUserBlock();
                block.setBlockUserId(user.getBuyerUserId())
                        .setBlockType(param.getBlockType())
                        .setBlockTargetId(param.getBlockTargetId());
                save(block);
                //todo: 删除用户和屏蔽用户之间的关系

            }
            throw new CoBusinessException(CoReturnFormat.FAIL, "已存在该屏蔽目标");
        } else {
            // 删除屏蔽（取消屏蔽）
            removeById(param.getBlockId());
        }
    }
}




